﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Command;
using Structure.Interfaces;

namespace Application
{
    public partial class ProjectList : System.Web.UI.UserControl
    {
        public event Action<string> onEdit;
        public event Action<string> onUser;
        private iCommand _command = BuildCommand.Build();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    // добавляем кнопки удаления          
                    DataControlField DeleteButtons = new ButtonField();
                    (DeleteButtons as ButtonField).CommandName = "DeleteRow";
                    (DeleteButtons as ButtonField).ButtonType = ButtonType.Image;
                    (DeleteButtons as ButtonField).ImageUrl = "~/images/delete.png";
                    ProjectsGridView.Columns.Add(DeleteButtons);

                    // добавляем кнопки редактирования
                    DataControlField EditButtons = new ButtonField();
                    (EditButtons as ButtonField).CommandName = "EditRow";
                    (EditButtons as ButtonField).ButtonType = ButtonType.Image;
                    (EditButtons as ButtonField).ImageUrl = "~/images/edit.gif";
                    ProjectsGridView.Columns.Add(EditButtons);

                    // добавляем кнопки добавления пользователей
                    DataControlField UsersButtons = new ButtonField();
                    (UsersButtons as ButtonField).CommandName = "UserInProject";
                    (UsersButtons as ButtonField).ButtonType = ButtonType.Image;
                    (UsersButtons as ButtonField).ImageUrl = "~/images/user.png";
                    ProjectsGridView.Columns.Add(UsersButtons);

                    //ProjectsGridView.AllowSorting = true;
                    //ProjectsGridView.DataSource = _command.ProjectList();
                    //ProjectsGridView.DataBind();
                }

                // параметры таблицы
                ProjectsGridView.AllowSorting = true;

                // получаем список проектов
                //     iCommand _command;
                //      _command = BuildCommand.Build();

                ProjectsGridView.DataSource = _command.ProjectList();
                ProjectsGridView.DataBind();

            }
            catch (Exception ex)
            { }
        }

        protected void ProjectsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "DeleteRow")
                {
                    int index = Convert.ToInt32(e.CommandArgument);

                    DataTable dt = ProjectsGridView.DataSource as DataTable;

                    string id_project = dt.Rows[index]["#"].ToString();

                    _command.DeleteProject(id_project);                 

                    dt.Rows.RemoveAt(index);
                    
                    ProjectsGridView.DataSource = dt;
                    ProjectsGridView.DataBind();

                }
                else if (e.CommandName == "EditRow")
                {
                    int index = Convert.ToInt32(e.CommandArgument);

                    DataTable dt = ProjectsGridView.DataSource as DataTable;

                    string id_project = dt.Rows[index]["#"].ToString();

                    onEdit(id_project);

                    //Response.AddHeader("id_project", id_project);
                    //Response.AddHeader("action", "edit");
                    //Response.Redirect("~/Settings/ProjectForm.aspx");
                }
                else if (e.CommandName == "UserInProject")
                {
                    int index = Convert.ToInt32(e.CommandArgument);

                    DataTable dt = ProjectsGridView.DataSource as DataTable;

                    string id_project = dt.Rows[index]["#"].ToString();

                    onUser(id_project);
                }
            }

            catch (Exception ex)
            {}
        }

        protected void ProjectsGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            { }

            catch (Exception ex)
            { }
        }

        public void Update()
        {
            ProjectsGridView.DataSource = _command.ProjectList();
            ProjectsGridView.DataBind();
        }

        protected void btSelectProject_Click(object sender, EventArgs e)
        {
            int id_project = Convert.ToInt32(SelectProject.Text);
            ProjectsGridView.DataSource = _command.SelectProject(id_project);
            ProjectsGridView.DataBind();
        }
    }
}